#pragma config(Hubs,  S1, HTMotor,  HTMotor,  HTServo,  none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Motor,  motorA,           ,             tmotorNXT, openLoop)
#pragma config(Motor,  motorB,           ,             tmotorNXT, openLoop)
#pragma config(Motor,  motorC,           ,             tmotorNXT, openLoop)
#pragma config(Motor,  mtr_S1_C1_1,     MiddleMtr,     tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C1_2,     motorE,        tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C2_1,     LeftMtr,       tmotorTetrix, openLoop, encoder)
#pragma config(Motor,  mtr_S1_C2_2,     RightMtr,      tmotorTetrix, openLoop, encoder)
#pragma config(Servo,  srvo_S1_C3_1,    servo1,               tServoStandard)
#pragma config(Servo,  srvo_S1_C3_2,    servo2,               tServoStandard)
#pragma config(Servo,  srvo_S1_C3_3,    servo3,               tServoStandard)
#pragma config(Servo,  srvo_S1_C3_4,    servo4,               tServoStandard)
#pragma config(Servo,  srvo_S1_C3_5,    servo5,               tServoStandard)
#pragma config(Servo,  srvo_S1_C3_6,    servo6,               tServoStandard)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                           Tele-Operation Mode Code Template
//
// This file contains a template for simplified creation of an tele-op program for an FTC
// competition.
//
// You need to customize two functions with code unique to your specific robot.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

#include "JoystickDriver.c"  //Include file to "handle" the Bluetooth messages.

//#pragma debuggerWindows("joystickSimple");

#define SERVO_START_POS 0
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                    initializeRobot
//
// Prior to the start of tele-op mode, you may want to perform some initialization on your robot
// and the variables within your program.
//
// In most cases, you may not have to add any code to this function and it will remain "empty".
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

void initializeRobot()
{
	servo[servo1] = SERVO_START_POS;
	servo[servo2] = SERVO_START_POS;
	motor[LeftMtr] = 0;
	motor[RightMtr] = 0;
	motor[MiddleMtr] = 0;
  // Place code here to sinitialize servos to starting positions.
  // Sensors are automatically configured and setup by ROBOTC. They may need a brief time to stabilize.

  return;
}


/////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                         Main Task
//
// The following is the main code for the tele-op robot operation. Customize as appropriate for
// your specific robot.
//
// Game controller / joystick information is sent periodically (about every 50 milliseconds) from
// the FMS (Field Management System) to the robot. Most tele-op programs will follow the following
// logic:
//   1. Loop forever repeating the following actions:
//   2. Get the latest game controller / joystick settings that have been received from the PC.
//   3. Perform appropriate actions based on the joystick + buttons settings. This is usually a
//      simple action:
//      *  Joystick values are usually directly translated into power levels for a motor or
//         position of a servo.
//      *  Buttons are usually used to start/stop a motor or cause a servo to move to a specific
//         position.
//   4. Repeat the loop.
//
// Your program needs to continuously loop because you need to continuously respond to changes in
// the game controller settings.
//
// At the end of the tele-op period, the FMS will autonmatically abort (stop) execution of the program.
//
///////////////////////////////////////////////////////////////////////////////////////////////////

task main()
{
  int threshold = 20;             /* Int 'threshold' will allow us to ignore low       */
  initializeRobot();

  waitForStart();     // wait for start of tele-op phase

  while (true)
  {
	  ///////////////////////////////////////////////////////////
	  ///////////////////////////////////////////////////////////
	  ////                                                   ////
	  ////      Add your robot specific tele-op code here.   ////
	  ////                                                   ////
	  ///////////////////////////////////////////////////////////
	  ///////////////////////////////////////////////////////////

    // Insert code to have servos and motors respond to joystick and button values.

    // Look in the ROBOTC samples folder for programs that may be similar to what you want to perform.
    // You may be able to find "snippets" of code that are similar to the functions that you want to
    // perform.
      getJoystickSettings(joystick);

     if(abs(joystick.joy1_y1) > threshold)   // If the left analog stick's Y-axis readings are either above or below the threshold:
    {
    	motor[RightMtr] = joystick.joy1_y1;         // Motor C is assigned a power level equal to the left analog stick's Y-axis reading.
    }
    else                                    // Else if the readings are within the threshold:
    {
      motor[RightMtr] = 0;                        // Motor C is stopped with a power level of 0.
    }
    if(abs(joystick.joy1_y2) > threshold)
    {
    	motor[LeftMtr] = joystick.joy1_y2;
 		}
    else                                    // Else if the readings are within the threshold:
    {
      motor[LeftMtr] = 0;                        // Motor C is stopped with a power level of 0.
    }
    if(joy1Btn(8))
    {
    	motor[MiddleMtr] = 100;
    }
    else if(joy1Btn(7))
    {
    	motor[MiddleMtr] = -100;
    }
    else
    {
    	motor[MiddleMtr] = 0;
    }
    if (joystick.joy1_TopHat == 0)
    {
    	servo[servo1] = SERVO_START_POS-25;
    }
    else if (joystick.joy1_TopHat == 4)
    {
    	servo[servo1] = SERVO_START_POS+25;
    }
    else
    {
    	servo[servo1] = 0;
    }
    if (joystick.joy1_TopHat == 2)
    {
    	servo[servo2] = SERVO_START_POS-25;
    }
    else if (joystick.joy1_TopHat == 6)
    {
    	servo[servo2] = SERVO_START_POS+25;
    }
    else
    {
    	servo[servo2] = 0;
    }
    if (joy1Btn(4))
    {
    	servo[servo3] = SERVO_START_POS-25;
    }
    else if (joy1Btn(2))
    {
    	servo[servo3] = SERVO_START_POS+25;
    }
    else
    {
    	servo[servo3] = 0;
    }
    if (joy1Btn(3))
    {
    	servo[servo4] = SERVO_START_POS-25;
    }
    else if (joy1Btn(1))
    {
    	servo[servo4] = SERVO_START_POS+25;
    }
    else
    {
    	servo[servo4] = 0;
    }
	}
}
